// pages/zhifuye/zhifuye.js
const app = getApp();
import {
  areaList
} from '@vant/area-data';
Page({

  /**
   * 页面的初始数据
   */
  data: {

    zongsuju: '', //详情数据
    SALES_TYPE: '', //判断是否购买
    GOODS_CLASSIFICATION: '', //判断是否是虚拟商品
    CUSTOMER_TEL: '', //客户手机号
    GOODS_ID: '', //商品单号
    ADDRESS_ID: '', //返回的地址ID
    openid: '',
    MESSAGE: '',
    ORDER_NUMBER: '',
    ORDER_PRICE_ZERO: '',
    prepay_id: '',
    nonceStr: '',
    package: '',
    paySign: '',
    signType: '',
    timeStamp: '',


    CUSTOMER_ID: '', //用户id

    areaList,
    num: 1,
    minusStatus: '1',
    GOODS_INTEGRAL:"",//积分
    PAYMENT_AMOUNT: '', //变化价格
    jiaaa: '', //初始价格
    CONSIGNEE_ADDRESS: '', //内容
    show: false,
    showsss: false,
    shengji: '',
    shiji: '',
    xianji: '',
    dizhilan: true,

    kehudcizhi: '', //地址
    yiyoudizhi: true,

    xiangxi: '',

    asdfg:true,
  },


  xiandi() {
    this.setData({
      dizhilan: false
    })
  },

  dizhiding() {

    this.setData({
      openid: app.globalData.openid
    });

    if (this.data.CUSTOMER_TEL != '' || this.data.shengji != '' || this.data.CONSIGNEE_ADDRESS != '') {
      this.setData({
        dizhilan: true
      })
      wx.showToast({
        title: '地址填写成功',
        icon: 'success',
        duration: 2000
      })
    } else {
      this.setData({
        dizhilan: true
      })
      wx.showToast({
        title: '请完善信息',
        icon: 'error',
        duration: 2000
      })
    }
  },


  quxiao() {
    this.setData({
      show: false
    });
  },

  queding(e) {
    console.log(e);
    console.log(e.detail.values[0].name);
    console.log(e.detail.values[1].name);
    console.log(e.detail.values[2].name);
    this.setData({
      show: false,
      shengji: e.detail.values[0].name,
      shiji: e.detail.values[1].name,
      xianji: e.detail.values[2].name,
    });
  },


  inptext(e) {
    this.setData({
      CONSIGNEE_ADDRESS: e.detail.value
    })
  },


  xuanzhedizhi(e) {
    console.log(e);
    this.setData({
      asdfg:true,


    })
  },




  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },



  // /* 点击减号 */
  // bindMinus: function () {
  //   var num = this.data.num;
  //   if (num > 1) {
  //     num--;
  //     this.setData({
  //       num: num,
  //       minusStatus: num,
  //       PAYMENT_AMOUNT: (this.data.PAYMENT_AMOUNT * 1) - (this.data.jiaaa * 1)
  //     });
  //   }

  //   console.log(this.data.minusStatus);
  //   console.log(this.data.PAYMENT_AMOUNT);
  // },
  // /* 点击加号 */
  // bindPlus: function () {
  //   var num = this.data.num;
  //   num++;
  //   this.setData({
  //     num: num,
  //     minusStatus: num,
  //     PAYMENT_AMOUNT: (this.data.PAYMENT_AMOUNT * 1) + (this.data.jiaaa * 1)
  //   });
  //   console.log(this.data.minusStatus);
  //   console.log(this.data.PAYMENT_AMOUNT);
  // },


  purchase() {

    if (this.data.CUSTOMER_TEL == '' || this.data.shengji == '' || this.data.CONSIGNEE_ADDRESS == '') {
      wx.showToast({
        title: '请填写地址',
        icon: 'error',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.interface+'/newVersion/API_2015_exchangeGoods',
        method: 'POST',
        data: {
          OPEN_ID: this.data.openid,
          GOODS_ID: this.data.GOODS_ID,
          CUSTOMER_TEL: this.data.CUSTOMER_TEL,
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data);
          if (res.data.result == 'success') {
            this.setData({
              CUSTOMER_TEL: '',
            })
            // wx.navigateBack({
            //   delta: 1
            // })

            wx.showToast({
              title: '兑换成功',
              icon: 'success',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: res.data.data,
              icon: 'error',
              duration: 2000
            })

          }
        }
      })
    }
  },


  // save() {
  //   console.log(1111);

  //   this.setData({
  //     CUSTOMER_ID: app.globalData.CUSTOMER_ID
  //   });



  //   if (this.data.CUSTOMER_TEL == '' || this.data.shengji == '' || this.data.CONSIGNEE_ADDRESS == '') {
  //     wx.showToast({
  //       title: '请填写地址',
  //       icon: 'error',
  //       duration: 2000
  //     })
  //   } else {
  //     wx.request({
  //       url: app.globalData.interface+'/newVersion/API_2071_saveShippingAddress',
  //       method: 'POST',
  //       data: {
  //         OPEN_ID: this.data.openid,
  //         CUSTOMER_TEL: this.data.CUSTOMER_TEL,
  //         SHOP_TYPE: this.data.GOODS_CLASSIFICATION,
  //         PROVINCE_NAME: this.data.shengji,
  //         CITY_NAME: this.data.shiji,
  //         DISTRICT_NAME: this.data.xianji,
  //         ADDRESS: this.data.CONSIGNEE_ADDRESS,
  //         REMARKS: '',
  //         OPEN_ID: this.data.openid, //用户OPEN_ID
  //         session_key: this.data.session_key,
  //         OPEN_UUID: this.data.jiami,
  //         CUSTOMER_ID: this.data.CUSTOMER_ID,
  //       },
  //       header: {
  //         'content-type': 'application/x-www-form-urlencoded'
  //       },
  //       success: res => {
  //         console.log(res.data);
  //         this.setData({
  //           ADDRESS_ID: res.data.shippingAddress.ADDRESS_ID
  //         })

  //         if (res.data.result == 'success') {
  //           this.pay()
  //         }
  //       }
  //     })
  //   }
  // },





  pay: function () {


    if (this.data.CUSTOMER_TEL == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'error',
        duration: 2000
      })
    } else {
      console.log(this.data.openid);
      console.log(111);
      // this.gitxin() 
      wx.request({
        url: app.globalData.interface+'/newVersion/API_1070_weiXinPayment',
        method: 'POST',
        data: {
          GOODS_ID: this.data.GOODS_ID,
          ADDRESS_ID: this.data.ADDRESS_ID,
          PAYMENT_AMOUNT: this.data.PAYMENT_AMOUNT,
          OPEN_ID: this.data.openid,
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          this.setData({
            MESSAGE: res.data.MESSAGE,
            ORDER_NUMBER: res.data.ORDER_NUMBER,
            ORDER_PRICE_ZERO: res.data.ORDER_PRICE_ZERO,
            prepay_id: res.data.prepay_id,
            nonceStr: res.data.MESSAGE.nonceStr,
            package: res.data.MESSAGE.package,
            paySign: res.data.MESSAGE.paySign,
            signType: res.data.MESSAGE.signType,
            timeStamp: res.data.MESSAGE.timeStamp,
          })
          console.log(res.data.MESSAGE.nonceStr);
          if (!res.data.MESSAGE.nonceStr || !res.data.MESSAGE.package || !res.data.MESSAGE.paySign) {
            wx.showToast({
              title: '付款失败',
              icon: 'loding',
              duration: 2000
            })
          } else {

            wx.requestPayment({
              'timeStamp': this.data.timeStamp,
              'nonceStr': this.data.nonceStr,
              'package': this.data.package,
              'signType': this.data.signType,
              'paySign': this.data.paySign,
              'success': function (res) {
                console.log('支付返回数据', res)

                setTimeout(() => {
                  wx.navigateTo({
                    url: '../../pages/orders/orders?data=' + null
                  })
                }, 1000)

                wx.hideLoading({
                  success: (res) => {
                    console.log(res);
                    wx.showToast({
                      title: '付款成功',
                      icon: 'loding',
                      duration: 2000
                    })

                  },
                })
              },
              'fail': function (res) {
                wx.showToast({
                  title: '付款失败',
                  icon: 'error',
                  duration: 2000
                })
                // wx.redirectTo({
                //   url: '../order/order?currentIndex=' + 1 + '&formUrl=' + 1 + '&formOrder=2',
                // })
              },
              // complete: function () {
              //   this.setData({
              //     hiddenLoading: true,
              //     submitOrder: true
              //   })
              // }
            })
            console.log(this.data.timeStamp);
            console.log(this.data.nonceStr);
            console.log(this.data.package);
            console.log(this.data.signType);
            console.log(this.data.paySign);
          }
          // console.log(JSON.stringify(res.data.myPrizeList));
        }

      })

    }


  },




  dizhi() {

    wx.request({
      url: app.globalData.interface+'/newVersion/API_2073_selectShippingAddress',
      method: 'POST',
      data: {

        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {

        console.log(res.data.data);
        if (res.data.result == 'success') {
          this.setData({
            yiyoudizhi: false,
          })
          this.setData({
            kehudcizhi: res.data.data
          })

        } else {
          this.setData({
            yiyoudizhi: true,
          })
          wx.showToast({
            title: res.data.data,
            icon: 'error',
            duration: 2000
          })
        }
      }
    })

  },

  xuanyoudi() {


    this.dizhi()

    this.setData({
      showsss: true,
    })




  },


  xuanzexiang(e) {

    console.log(e.currentTarget.dataset.item);
    this.setData({
      showsss: false,
      yiyoudizhi: true,
      shengji: e.currentTarget.dataset.item.PROVINCE_NAME,
      shiji: e.currentTarget.dataset.item.CITY_NAME,
      xianji: e.currentTarget.dataset.item.DISTRICT_NAME,
      CUSTOMER_TEL: e.currentTarget.dataset.item.CUSTOMER_TEL,
      CONSIGNEE_ADDRESS: e.currentTarget.dataset.item.ADDRESS,
    })

  },





  xinzendizhi(){
    wx.navigateTo({
      url: '../../pages/shouhuodizhi/shouhuodizhi',
    })

    
    this.setData({
      showsss: false,
    })



  },



  morendizhi() {

    wx.request({
      url: app.globalData.interface+'/newVersion/API_2073_selectShippingAddress',
      method: 'POST',
      data: {

        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {

        console.log(res.data.data[0]);
        if (res.data.result == 'success') {
    
          this.setData({
     
            shengji: res.data.data[0].PROVINCE_NAME,
            shiji: res.data.data[0].CITY_NAME,
            xianji: res.data.data[0].DISTRICT_NAME,
            CONSIGNEE_ADDRESS: res.data.data[0].ADDRESS,
            CUSTOMER_TEL: res.data.data[0].CUSTOMER_TEL,
          })

        } else {

          wx.showToast({
            title: res.data.data,
            icon: 'error',
            duration: 2000
          })
        }
      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(e);
    console.log(JSON.parse(e.data));

    var zongsuju = JSON.parse(e.data)

    this.setData({
      zongsuju: zongsuju,
      SALES_TYPE: zongsuju.SALES_TYPE,
      PAYMENT_AMOUNT: zongsuju.GOODS_PRICE, //价格
      GOODS_INTEGRAL: zongsuju.GOODS_INTEGRAL, //积分
      jiaaa: zongsuju.GOODS_PRICE,
      GOODS_CLASSIFICATION: zongsuju.GOODS_CLASSIFICATION, //判断是否是虚拟商品
      GOODS_ID: zongsuju.GOODS_ID, //商品ID

    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {




    this.setData({

      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    });

this.morendizhi()


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})